<template>
	<view class="uni-container">
		<uni-forms ref="form" :model="formData" validate-trigger="submit" err-show-type="toast">
			<uni-forms-item name="enterprise_in" label="单位" required>
				<uni-data-picker v-model="formData.enterprise_in" collection="enterprise"
					field="USCC as value, enterprise_Name as text"></uni-data-picker>
			</uni-forms-item>
			<uni-forms-item name="month" label="月份" required>
				<uni-easyinput v-model="formData.month"></uni-easyinput>
			</uni-forms-item>
			<uni-forms-item name="runstatus" label="运营状态" required>
				<uni-data-checkbox v-model="formData.runstatus"
					:localdata="formOptions.runstatus_localdata"></uni-data-checkbox>
			</uni-forms-item>
			<uni-forms-item name="worknum" label="从业人员数" required>
				<uni-easyinput placeholder="从业人员数" v-model="formData.worknum"></uni-easyinput>
			</uni-forms-item>
			<uni-forms-item name="totolmoney" label="旅游总收入" required>
				<uni-easyinput placeholder="旅游总收入" v-model="formData.totolmoney"></uni-easyinput>
			</uni-forms-item>
			<uni-forms-item name="tikitmoney" label="门票收入" required>
				<uni-easyinput placeholder="门票收入" v-model="formData.tikitmoney"></uni-easyinput>
			</uni-forms-item>
			<uni-forms-item name="jiedaipeoplenum" label="接待游客人次" required>
				<uni-easyinput placeholder="接待游客人次" v-model="formData.jiedaipeoplenum"></uni-easyinput>
			</uni-forms-item>
			<uni-forms-item name="freetikitnum" label="免票人次" required>
				<uni-easyinput placeholder="免票人次" v-model="formData.freetikitnum"></uni-easyinput>
			</uni-forms-item>
			<view class="uni-button-group">
				<button type="primary" class="uni-button" @click="submit">提交</button>
			</view>
		</uni-forms>
	</view>
</template>

<script>
	import {
		validator
	} from '../../js_sdk/validator/testbiao.js';

	const db = uniCloud.database();
	const dbCollectionName = 'zhongdianjiancedanweibiao';

	function getValidator(fields) {
		let result = {}
		for (let key in validator) {
			if (fields.indexOf(key) > -1) {
				result[key] = validator[key]
			}
		}
		return result
	}



	export default {
		data() {
			let formData = {
				"enterprise_in": "",
				"month": 1,
				"runstatus": 0,
				"worknum": "",
				"totolmoney": "",
				"tikitmoney": "",
				"jiedaipeoplenum": "",
				"freetikitnum": "",
				"reportID": null,
				"userid": "",
				"_id":""
			}

			return {

				formData,
				formOptions: {
					"runstatus_localdata": [{
							"text": "正常运营",
							"value": 0
						},
						{
							"text": "停业（歇业）",
							"value": 1
						},
						{
							"text": "当年关闭",
							"value": 2
						},
						{
							"text": "当年破产",
							"value": 3
						}
					]
				},
				rules: {
					...getValidator(Object.keys(formData))
				}
			}
		},
		onLoad(options) {
			// 获取浏览器传递的参数
			
				this.formData.userid = options.userid; // 接收 reportID 参数
				this.formData._id = options._id;
				console.log(this.formData.userid)
				console.log(this.formData._id)
		},
		onReady() {
			this.$refs.form.setRules(this.rules)
		},
		methods: {

			/**
			 * 验证表单并提交
			 */
			submit() {
				uni.showLoading({
					mask: true
				})
				this.$refs.form.validate().then((res) => {
					return this.submitForm(res)
				}).catch(() => {}).finally(() => {
					uni.hideLoading()
				})
			},

			/**
			 * 提交表单
			 */
			submitForm(value) {

				console.log(this.formData.userid)

				return db.collection(dbCollectionName).add(value).then((res) => {
					uni.showToast({
						icon: 'none',
						title: '新增成功'
					})
					db.collection("a-userxiafabiaodan").where({
						_id:this.formData._id,
						nickname: this.formData.userid
					}).update({
						
						tianxie_status: "已填写"
					})
					this.getOpenerEventChannel().emit('refreshData')
					setTimeout(() => uni.navigateBack(), 500)


				}).catch((err) => {
					uni.showModal({
						content: err.message || '请求服务失败',
						showCancel: false
					})
				})
				// let test=db.collection("testbiao").field("reportid").getTemp();
				// let user=db.collection("uni-id-users").field("_id,username").getTemp();
				// db.collection(test,user).get().then(res=>{
				// 	console.log(res)
				// })
			}
		}
	}
</script>

<style>
	.uni-container {
		padding: 15px;
	}

	.uni-input-border,
	.uni-textarea-border {
		width: 100%;
		font-size: 14px;
		color: #666;
		border: 1px #e5e5e5 solid;
		border-radius: 5px;
		box-sizing: border-box;
	}

	.uni-input-border {
		padding: 0 10px;
		height: 35px;

	}

	.uni-textarea-border {
		padding: 10px;
		height: 80px;
	}

	.uni-button-group {
		margin-top: 50px;
		/* #ifndef APP-NVUE */
		display: flex;
		/* #endif */
		justify-content: center;
	}

	.uni-button {
		width: 184px;
	}
</style>